<?php

// 参考：http://www.php.net/manual/zh/pdo.connections.php
// https://segmentfault.com/a/1190000004106175

include_once 'db_config.php';
// array(PDO::ATTR_PERSISTENT => true) 开启持久化连接
$dbh = new PDO('mysql:host=localhost;dbname=wx', $db['user'], $db['pwd'], array(PDO::ATTR_PERSISTENT => true));

// 获取初始年月到当前年月日 http://www.itnose.net/detail/6066165.html
if (empty($_POST["date_start"]) & empty($_POST["date_end"])) {
    // 获取指定日期的净增用户总量
    $next_day = date("Y-m-d", strtotime('-14 days'));
    $end_day = date('Y-m-d', strtotime('-1 days'));
} else {
    $next_day = $_POST['date_start'];
    $end_day = $_POST['date_end'];
}
$sql = 'SELECT * FROM `stat` where date>="' . $next_day . '" and date<="' . $end_day . '"';
foreach ($dbh->query($sql) as $row) {
    // 把日期和用户数单独组成列 | 变量追加到数组中
    $arr['date'][] = $row['date'];
    $arr['uv'][] = $row['uv'];
    $arr['cost'][] = $row['cost'];
    $arr['se_key'][] = $row['se_key'];
    $arr['total_key'][] = $row['total_key'];
}
//var_dump($arr);
$data = json_encode($arr);
echo $data;

// 现在运行完成，在此关闭连接
$dbh = null;
?>